package com.mingmen.supplier.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.CommodityAdapter;
import com.mingmen.supplier.adapter.CommodityLabelAdapter;
import com.mingmen.supplier.adapter.NewOrderDetailAdapter;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.defineview.NewOrderDialog;

import com.mingmen.supplier.defineview.WeihuDialog;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.label.bean.LabelTestBean;
import com.mingmen.supplier.label.recycler.OnItemClickListener;
import com.mingmen.supplier.model.EventBean;
import com.mingmen.supplier.model.NewOrderDetailModel;
import com.mingmen.supplier.popwindow.PopupWindowCompat;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.SharePreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

public class NewOrderDetailActivity extends AppCompatActivity implements NewOrderDetailAdapter.OnItemClickListener {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;


    @BindView(R.id.btn_receive)
    Button btn_login;

    @BindView(R.id.btn_reject)
    Button btn_reject;
    @BindView(R.id.layout_Distribution)
    LinearLayout layout_Distribution;
    String token;
    String billId;
    @BindView(R.id.recycle_detail)
    RecyclerView recycle_detail;

    @BindView(R.id.restaurantName)
    TextView restaurantName;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.orderDate)
    TextView orderDate;
    @BindView(R.id.shipping)
    TextView shipping;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.applyMoney)
    TextView applyMoney;
    @BindView(R.id.text_billId)
    TextView text_billId;
    List<NewOrderDetailModel.DataBean.BillDetailsBean> dataBeanList = new ArrayList<>();
    NewOrderDetailAdapter newOrderDetailAdapter;
    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    private int resurtResurant = 100;
    String myShipping = "";
    String shippingFee = "";
    String partCancelFlag;
    String partArriveFlag;
    String partRefuseFlag;
    String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_neworder);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        token = (String) SharePreferenceUtil.get(this, "token", "");
        billId = getIntent().getStringExtra("billId");
        partCancelFlag = getIntent().getStringExtra("partCancelFlag");
        partArriveFlag = getIntent().getStringExtra("partArriveFlag");
        partRefuseFlag = getIntent().getStringExtra("partRefuseFlag");
        recycle_detail.setLayoutManager(new LinearLayoutManager(this));
        recycle_detail.setNestedScrollingEnabled(false);
        newOrderDetailAdapter = new NewOrderDetailAdapter(this, dataBeanList);
        recycle_detail.setAdapter(newOrderDetailAdapter);
        newOrderDetailAdapter.setOnItemClickListener(this);
        flag=getIntent().getStringExtra("flag");
        HttpResponse.getOrder(token, billId, partCancelFlag, partArriveFlag, partRefuseFlag, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    NewOrderDetailModel newOrderDetailModel = new NewOrderDetailModel();
                    newOrderDetailModel = GsonUtil.GsonToBean(s, NewOrderDetailModel.class);
                    restaurantName.setText(newOrderDetailModel.getData().get(0).getRestaurantName());
                    address.setText(newOrderDetailModel.getData().get(0).getAddress());
                    orderDate.setText(newOrderDetailModel.getData().get(0).getOrderDate());
                    if (newOrderDetailModel.getData().get(0).getShipping().equals("0")) {
                        shipping.setText("线下自取");
                    } else {
                        shipping.setText("商家配送");
                    }
                    total.setText(newOrderDetailModel.getData().get(0).getTotal() + "元");
                    applyMoney.setText("(￥" + newOrderDetailModel.getData().get(0).getApplyMoney() + "+配送费: " + "￥" + newOrderDetailModel.getData().get(0).getShippingFee() + "元" + ")");
                    text_billId.setText("订单编号: " + newOrderDetailModel.getData().get(0).getBillId());
                    dataBeanList.addAll(newOrderDetailModel.getData().get(0).getBillDetails());
                    myShipping = newOrderDetailModel.getData().get(0).getShipping();
                    shippingFee = newOrderDetailModel.getData().get(0).getShippingFee();
                    newOrderDetailAdapter.setDataList(dataBeanList);
                } else {
                    Toast.makeText(NewOrderDetailActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    @OnClick({R.id.text_return, R.id.btn_receive, R.id.btn_reject, R.id.layout_Distribution, R.id.linear_return})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_receive:
                List<String> jdatas = new ArrayList<>();
                JSONObject jsonObject = new JSONObject();


                for (int i = 0; i < dataBeanList.size(); i++) {
                    try {
                        jsonObject.put("billDetailId", dataBeanList.get(i).getBillDetailId());
                        jsonObject.put("confirmPrice", dataBeanList.get(i).getShijia());
                        jsonObject.put("confirmAmount", dataBeanList.get(i).getConfirmAmount());
                        jdatas.add(jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Log.d("jdatasjdatas", jdatas.toString());

                HttpResponse.receivingOrder(token, billId, myShipping, shippingFee, jdatas.toString(), "0", new StringCallback(this) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        BaseBean baseBean = new BaseBean();
                        baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                        if (baseBean.getCode() == 0) {
                            EventBus.getDefault().post(7);
                            finish();

                        }
                        Toast.makeText(NewOrderDetailActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                    }
                });


                break;
            case R.id.btn_reject:

                HttpResponse.rejectOrder(token, billId, new StringCallback(this) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BaseBean baseBean = new BaseBean();
                        baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                        if (baseBean.getCode() == 0) {
                            EventBus.getDefault().post(7);
                            finish();
                        }
                        Toast.makeText(NewOrderDetailActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                    }
                });

                break;
            case R.id.layout_Distribution:
//                startActivity(new Intent(NewOrderDetailActivity.this, DistributionModeActivity.class));
                intent = new Intent(NewOrderDetailActivity.this, DistributionModeActivity.class);
                intent.putExtra("shipping", myShipping);
                intent.putExtra("shippingFee", shippingFee);
                intent.putExtra("flag","0");
                startActivityForResult(intent, resurtResurant);

                break;
        }
    }


    @Override
    public void onItemClickListener(View view, int pos, List<NewOrderDetailModel.DataBean.BillDetailsBean> myLiveList) {
        NewOrderDetailModel.DataBean.BillDetailsBean billDetailsBean = myLiveList.get(pos);
        switch (view.getId()) {
            case R.id.confirmPrice:
                weihuDialog = new WeihuDialog(this);
                weihuDialog.setVerifyClickListener("价格", new WeihuDialog.VerifyClickListener() {
                    @Override
                    public void cancel() {
                    }

                    @Override
                    public void send(String str) {
                        myLiveList.get(pos).setConfirmPrice(str);
                        newOrderDetailAdapter.notifyDataSetChanged();
                    }
                });
                weihuDialog.show();
                break;
        }

    }

    String text_peiSong;
    WeihuDialog weihuDialog;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == resurtResurant) {

            myShipping = data.getStringExtra("shipping");
            shippingFee = data.getStringExtra("shippingFee");
            text_peiSong = data.getStringExtra("text_peiSong");
            shipping.setText(text_peiSong);
        }

    }

    public void onEventMainThread(Integer eventBean) {


    }
}




